package com.timothy.android;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import com.timothy.android.answer.Bar2DHorActivity;
import com.timothy.android.answer.Bar2DVerActivity;
import com.timothy.android.answer.Bar3DHorActivity;
import com.timothy.android.answer.Bar3DVerActivity;
import com.timothy.android.answer.CyliHorActivity;
import com.timothy.android.answer.CyliVerActivity;
import com.timothy.android.answer.Bar2DFFActivity;
import com.timothy.android.answer.NoGraphActivity;
import com.timothy.android.answer.Pie2DActivity;
import com.timothy.android.answer.Pie3DActivity;
import com.timothy.android.answer.SlideActivity;
import com.timothy.android.answer.UnderChoiceActivity;
import com.timothy.android.layout.LayoutA;
import com.timothy.android.layout.LayoutB;
import com.timothy.android.layout.LayoutC;
import com.timothy.android.layout.LayoutD;
import com.timothy.android.layout.LayoutE;
import com.timothy.android.layout.LayoutFreeForm;
import com.timothy.android.layout.LayoutGrading2;
import com.timothy.android.layout.LayoutH;
import com.timothy.android.layout.LayoutI;
import com.timothy.android.layout.LayoutMultiSelect;
import com.timothy.android.layout.LayoutPriority;
import com.timothy.android.layout.LayoutSlide;
import com.timothy.android.layout.LayoutX;
import com.timothy.android.util.XMLUtil;
import com.timothy.service.CommunicationService;
import com.timothy.service.MessageSenderUtil;

public class BaseProcessActivity extends BaseActivity {

	private static final String LOG_TAG = "BaseProcessActivity";
	
	protected String hostIp = null;
	protected int hostPort ;
	protected String baseStationStr = null;
	
/*	public void appendTitle(String append){
    	this.setTitle(getTitle()+append);
    }*/
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i(LOG_TAG, "onCreate");
		
	    //get phone ip and port
		Bundle bundle = this.getIntent().getExtras();
		hostIp = bundle.getString("hostIp");
		if(hostIp!=null){
			Log.i(LOG_TAG,"hostIp="+hostIp);
		}
		
		hostPort = bundle.getInt("hostPort");
		if(hostIp!=null){
			Log.i(LOG_TAG,"hostPort="+hostPort);
		}
		
		baseStationStr = bundle.getString("baseStationStr");
		if(hostIp!=null){
			Log.i(LOG_TAG,"baseStationStr="+baseStationStr);
		}
		
		
		//set orientation
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
/*	protected void processMessageForPirority(int what, String content,Integer displayPriority) {
		Log.i(LOG_TAG,"processMessageForPirority()..");
		Log.i(LOG_TAG,"displayPriority:"+displayPriority);
		
		this.displayPriority = displayPriority;
		processMessage(what,content);
		
	}*/
	
	protected Integer displayPriority = null;
	
	
	protected Integer pageIndex = null;
	
	@Deprecated
	protected void processMessageWithPage(int what, String content,Integer pageIndex) {
		Log.i(LOG_TAG,"---------------------------processMessageWithPage()...---------------------------");
		Log.i(LOG_TAG,"what:"+what);
		Log.i(LOG_TAG,"pageIndex:"+pageIndex);
		this.pageIndex = pageIndex;
		processMessage(what,content);
		Log.i(LOG_TAG,"---------------------------processMessageWithPage()... END---------------------------");
	}
	
	
	
	@Override
	protected void processMessage(int what, String content) {

		if (CommunicationService.MSG_SLIDE_RESULT == what){
			Intent intent = new Intent();
			intent.putExtra("message", content);
			Log.i(LOG_TAG,content);
			
			String displayStyle = XMLUtil.getPropertyValue( content , "DisplayStyle");
			String type = XMLUtil.getPropertyValue( content , "Type");
			
			if(displayStyle!=null){
				Log.i(LOG_TAG,displayStyle);
				intent.putExtra("displayStyle", displayStyle);
			}
			
			if(type!=null){
				Log.i(LOG_TAG,type);
				intent.putExtra("type", type);
			}	
			
			if(pageIndex!=null){
				Log.i(LOG_TAG,type);
				intent.putExtra("pageIndexStr",  String.valueOf(pageIndex));
			}
			
			intent.putExtra("baseStationStr", baseStationStr);
			intent.putExtra("hostIp", hostIp);
			intent.putExtra("hostPort", hostPort);
			
			if("41".equals(type)){
				intent.setClass( getBaseContext(), SlideActivity.class);
			}else if("43".equals(type)){
				intent.setClass( getBaseContext(), Bar2DFFActivity.class);
			}else{
				switch(Integer.valueOf(displayStyle)){
					case 1:intent.setClass( getBaseContext(), Bar2DVerActivity.class);break;
					case 2:intent.setClass( getBaseContext(), Bar2DHorActivity.class);break;
					
					case 3:intent.setClass( getBaseContext(), Bar3DVerActivity.class);break;
					case 4:intent.setClass( getBaseContext(), Bar3DHorActivity.class);break;
					
					case 5:intent.setClass( getBaseContext(), CyliVerActivity.class);break;
					case 6:intent.setClass( getBaseContext(), CyliHorActivity.class);break;
					
					case 7:intent.setClass( getBaseContext(), Pie2DActivity.class);break;
					case 8:intent.setClass( getBaseContext(), Pie3DActivity.class);break;
					
					case 9:intent.setClass( getBaseContext(), UnderChoiceActivity.class);break;
					case 10:intent.setClass( getBaseContext(), NoGraphActivity.class);break;
					
					default:intent.setClass( getBaseContext(), LayoutError.class);
				}
				
				if("42".equals(type)&&displayPriority!=null){
					Log.i(LOG_TAG,"displayPriority:"+displayPriority);
					intent.putExtra("displayPriority", String.valueOf(displayPriority));
				}
				
			}
			
			//finish current activity
			Log.i(LOG_TAG,"finish current activity");
			Log.i(LOG_TAG,this.toString());
			this.finish();
			
			Log.i(LOG_TAG,"start new answer");
			startActivity(intent);
		}
		
		if (CommunicationService.MSG_QUESTION == what){
			Log.i(LOG_TAG,"CommonQuestion");
			Intent intent = new Intent();
			intent.putExtra("message", content);
			Log.i(LOG_TAG,"message:"+content);
			String typeStr = XMLUtil.getPropertyValue( content , "Type");
			Integer type = Integer.valueOf(typeStr);
			Log.i(LOG_TAG,"type:"+type);
			
			intent.putExtra("baseStationStr", baseStationStr);
			intent.putExtra("hostIp", hostIp);
			intent.putExtra("hostPort", hostPort);
			
			if(type>=1 && type<=5) {
				intent.setClass( getBaseContext(), LayoutA.class);//2 Buttons Question
			} else if ( type>=6 && type<=10 ) {
				intent.setClass( getBaseContext(), LayoutB.class);//3 Buttons Question
			} else if ( type>=11 && type<=13 ) {
				intent.setClass( getBaseContext(), LayoutC.class);//4 Buttons Question
			} else if ( type>=14 && type<=16 ) {
				intent.setClass( getBaseContext(), LayoutD.class);//5 Buttons Question
			} else if ( type>=17 && type<=21 ) {
				intent.setClass( getBaseContext(), LayoutE.class);//10 Buttons Question
			} else if ( type>=22 && type<=23 ) {
				intent.setClass( getBaseContext(), LayoutH.class);//Other number of buttons Question
			} else if ( type>=24 && type<=25 ) {
				intent.setClass( getBaseContext(), LayoutI.class);//Other number of buttons Question
			} else if ( type == 26 ) {
				intent.setClass( getBaseContext(), LayoutX.class);//Other number of buttons Question
			} else if ( type ==0 ) {
				intent.setClass( getBaseContext(), LayoutX.class);//Other number of buttons Question
			} else if ( type == 40 ) {
				intent.setClass( getBaseContext(), LayoutMultiSelect.class);
			} else if ( type == 41  ) {
				intent.setClass( getBaseContext(), LayoutSlide.class);
			} else if ( type == 42) {
				intent.setClass( getBaseContext(), LayoutPriority.class);
			} else if ( type == 43 ) {
				intent.setClass( getBaseContext(), LayoutFreeForm.class);
			} else if (  type == 45) {
				intent.setClass( getBaseContext(), LayoutGrading2.class);
			} else{
				intent.setClass( getBaseContext(), LayoutError.class);
			}
			
			//finish current activity
			Log.i(LOG_TAG,"finish current activity");
			Log.i(LOG_TAG,this.toString());
			this.finish();
			
			Log.i(LOG_TAG,"start new question");
			startActivity(intent);
		}
		
		if (CommunicationService.MSG_SHORT_MESSAGE == what){
		
		}
		
		if (CommunicationService.MSG_CLEAR == what){
			MessageSenderUtil.PC_IP =  hostIp;
			MessageSenderUtil.PC_PORT = hostPort;
//			MessageSenderUtil.PHONE_IP = getWifiIP();
			sendMessage(MessageSenderUtil.addHeader(MessageSenderUtil.Get_Base_Stations) );
		}
		
	}
	
	/*private String getWifiIP() {
		// get wifi service
		WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		// is wifi enabled
		if (!wifiManager.isWifiEnabled()) {//pending if needed
			wifiManager.setWifiEnabled(true);
		}
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		int ipAddress = wifiInfo.getIpAddress();
		String ip = intToIp(ipAddress);
		return ip;
	}*/

/*	private String intToIp(int i) {
		return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
				+ "." + (i >> 24 & 0xFF);
	}*/

}
